﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Controls;
using System.Windows.Data;
using System;
using DeepEarth.Map.Core;
using DeepEarth.Toolkit.Geometry.Data;
using System.Windows.Input;
namespace DeepEarth.Toolkit.Geometry
{
    public class Label : PointBase
    {
        #region Properties
        
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Label), new PropertyMetadata(Label.EmptyText, TextChangedCallback));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        static void TextChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            Label label = sender as Label;
            label.Text = (string)args.NewValue;

            if (args.NewValue == null || args.NewValue.ToString() == string.Empty)
            {
                label.Text = EmptyText;
            }
        }        
       
        public static readonly DependencyProperty TextBrushProperty =
            DependencyProperty.Register("TextBrush", typeof(Brush), typeof(Label), new PropertyMetadata(new SolidColorBrush(Colors.White), OnTextBrushChanged));

        public Brush TextBrush
        {
            get { return (Brush)GetValue(TextBrushProperty); }
            set { SetValue(TextBrushProperty, value); }
        }

        static void OnTextBrushChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            Label self = (Label)sender;

            if (args.NewValue != null)
            {
                SolidColorBrush scb = args.NewValue as SolidColorBrush;

                if (scb != null)
                {
                    byte alpha = scb.Color.A;
                    SolidColorBrush gb = self.GlowBrush as SolidColorBrush;

                    if (gb != null)
                    {
                        Color gbc = Color.FromArgb(alpha, gb.Color.R, gb.Color.G, gb.Color.B);
                        self.GlowBrush = new SolidColorBrush(gbc);
                    }
                }
            }
        }

        public static readonly DependencyProperty GlowBrushProperty =
            DependencyProperty.Register("GlowBrush", typeof(Brush), typeof(Label), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

        public Brush GlowBrush
        {
            get { return (Brush)GetValue(GlowBrushProperty); }
            set { SetValue(GlowBrushProperty, value); }
        }
    
        //protected override void ActiveChanged(bool isActive)
        //{
        //    if (this.ActiveTextColor != null)
        //        this.TextColor = this.IsActive ? this.ActiveTextColor : diabledColor;
        //}

        //protected override void SelectedChanged(bool isSelected)
        //{
        //    SyncEditableAndSelected(isSelected);
        //}

        //protected override void ScaleChanged(double newValue)
        //{
        //    if (this.Geometry != null)
        //    {
        //        LabelGeometry lg = this.Geometry as LabelGeometry;
        //        if (lg.FontSize != newValue)
        //        {
        //            lg.FontSize = newValue;
        //        }
        //    }
        //}

        //protected override void AdjustVisualScale(double newValue)
        //{
        //    double scale = Math.Pow(0.05 * (this.map.ZoomLevel + 2), 2) + 0.01;
        //    if (scale > 1) scale = 1;
        //    if (scale < 0.125) scale = 0.125;
        //    scale = scale * newValue * 10;

        //    this.FontSize = scale;
        //}

        #endregion

        public const string EmptyText = "Label";

        public Label()
        {
            this.EnableDrag = true;

            InitializeContent();

            DefaultStyleKey = typeof(Label);
        }

        void InitializeContent()
        {
            Grid g = new Grid();

            TextBlock displayBlock = new TextBlock();            
            TextBlock glowBlock = new TextBlock();
            TextBlock glowBlock2 = new TextBlock();
            TextBlock glowBlock3 = new TextBlock();
            TextBlock glowBlock4 = new TextBlock();

            Binding textBinding = new Binding("Text") { Source = this, Mode = BindingMode.TwoWay };
            displayBlock.SetBinding(TextBlock.TextProperty, textBinding);
            glowBlock.SetBinding(TextBlock.TextProperty, textBinding);
            glowBlock2.SetBinding(TextBlock.TextProperty, textBinding);
            glowBlock3.SetBinding(TextBlock.TextProperty, textBinding);
            glowBlock4.SetBinding(TextBlock.TextProperty, textBinding);

            Binding foregroundBinding = new Binding("TextBrush") { Source = this };
            displayBlock.SetBinding(TextBlock.ForegroundProperty, foregroundBinding);

            Binding glowBinding = new Binding("GlowBrush") { Source = this };
            glowBlock.SetBinding(TextBlock.ForegroundProperty, glowBinding);
            glowBlock2.SetBinding(TextBlock.ForegroundProperty, glowBinding);
            glowBlock3.SetBinding(TextBlock.ForegroundProperty, glowBinding);
            glowBlock4.SetBinding(TextBlock.ForegroundProperty, glowBinding);

            Binding fontSizeBinding = new Binding("FontSize") { Source = this, Mode=BindingMode.OneWay };
            displayBlock.SetBinding(TextBlock.FontSizeProperty, fontSizeBinding);
            glowBlock.SetBinding(TextBlock.FontSizeProperty, fontSizeBinding);
            glowBlock2.SetBinding(TextBlock.FontSizeProperty, fontSizeBinding);
            glowBlock3.SetBinding(TextBlock.FontSizeProperty, fontSizeBinding);
            glowBlock4.SetBinding(TextBlock.FontSizeProperty, fontSizeBinding);
              
            Binding opacityBinding = new Binding("Opacity") { Source = this };
            g.SetBinding(Grid.OpacityProperty, opacityBinding);

            displayBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            displayBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            TranslateTransform dropshadowTransform = new TranslateTransform();
            dropshadowTransform.X = -1;
            dropshadowTransform.Y = -1;
            glowBlock.RenderTransform = dropshadowTransform;

            TranslateTransform dropshadowTransform2 = new TranslateTransform();
            dropshadowTransform2.X = 1;
            dropshadowTransform2.Y = -1;
            glowBlock2.RenderTransform = dropshadowTransform2;

            TranslateTransform dropshadowTransform3 = new TranslateTransform();
            dropshadowTransform3.X = -1;
            dropshadowTransform3.Y = 1;
            glowBlock3.RenderTransform = dropshadowTransform3;

            TranslateTransform dropshadowTransform4 = new TranslateTransform();
            dropshadowTransform4.X = 1;
            dropshadowTransform4.Y = 1;
            glowBlock4.RenderTransform = dropshadowTransform4;

            g.Children.Add(glowBlock);
            g.Children.Add(glowBlock2);
            g.Children.Add(glowBlock3);
            g.Children.Add(glowBlock4);
            g.Children.Add(displayBlock);

            this.Content = g;

        }

    }

}